package com.sinocare.project.record.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sinocare.project.common.entity.SccDrugstore;
import com.sinocare.project.common.entity.SccPatient;
import com.sinocare.project.common.result.Result;
import com.sinocare.project.common.result.ResultCode;
import com.sinocare.project.record.service.SccPatientService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 检测者(用户/客户)表(SccPatient)表控制层
 *
 * @author makejava
 * @since 2021-12-21 15:53:05
 */
@RestController
@RequestMapping("sccPatient")
@CrossOrigin
public class SccPatientController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private SccPatientService sccPatientService;
    /**
     * 获取系统时间
     */
    Date date = new Date();
    /**
     * 初始化a
     */
    int a = 1;

    /**
     * 分页查询所有数据
     *
     * @param page       分页对象
     * @param searchParams 查询实体
     * @return 所有数据
     */
    @GetMapping
    public Result selectAll(Integer page , Integer limit, String searchParams) throws JsonProcessingException {
        QueryWrapper<SccPatient> wrapper = new QueryWrapper<>();
        if (!StrUtil.hasBlank(searchParams)){
            SccPatient sccPatient = JSON.parseObject(searchParams, SccPatient.class);
            wrapper.like(!sccPatient.getName().equals("")&& sccPatient.getName()!=null,"name",sccPatient.getName());
            wrapper.like(!sccPatient.getPhone().equals("")&& sccPatient.getPhone()!=null,"phone",sccPatient.getPhone());
            System.out.println(sccPatient);
        }
        PageHelper.startPage(page,limit);
        List<SccPatient> list = sccPatientService.list(wrapper);
        PageInfo<SccDrugstore> info = new PageInfo(list);
        return Result.ok(ResultCode.SUCCESS,info.getTotal(),info.getList());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result selectOne(@PathVariable Serializable id) {
        return Result.ok(ResultCode.SUCCESS,this.sccPatientService.getById(id));
    }

    /**
     * 新增数据 
     *
     * @param postData 实体对象
     * @return 新增结果
     */
    @PostMapping("ins")
    public Result insert(@RequestBody SccPatient postData) {
        //查询数据库最大账号
        QueryWrapper<SccPatient> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("max(custcmer_code) as count");
        SccPatient one = sccPatientService.getOne(queryWrapper);
        System.out.println(one);
        //取出最后一个数加1
        String count = one.getCustcmerCode().substring(4);
        int i = Integer.parseInt(count);
        ++i;
        //给会员号赋值
        postData.setCustcmerCode("4003"+i);
        //给创建时间赋值
        postData.setCreateTime(date);
        //给最后修改时间赋值
        postData.setLastTestTime(date);
        //给状态赋值
        postData.setStatus(1);
        //执行添加，并返回统一格式和结果
        return Result.ok(ResultCode.SUCCESS,this.sccPatientService.save(postData));
    }

    /**
     * 修改数据
     *
     * @param postData 实体对象
     * @return 修改结果
     */
    @PutMapping("upd")
    public Result update(@RequestBody SccPatient postData) {
        postData.setUpdateTime(date);
        return Result.ok(ResultCode.SUCCESS,this.sccPatientService.updateById(postData));
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public Result delete(@RequestParam("id") Integer id) {
        System.out.println(id);
        return Result.ok(ResultCode.SUCCESS,this.sccPatientService.removeById(id));
    }
}

